//
//  WBHomeViewController.swift
//  weiboswift
//
//  Created by dingzhibing on 2017/2/28.
//  Copyright © 2017年 dingzhibing. All rights reserved.
//

import UIKit

//定义全局常量，尽量定义成private
private let cellId = "cellId"
class WBHomeViewController: WBBaseViewController {
    
    fileprivate lazy var listViewModel = WBStatusListViewModel()
    
    //微博数据数组
    lazy var statusList = [String]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func loadData() {
        listViewModel.loadStatus(pullup: self.isPullup) { (isSuccess,shouldRefresh) in
            //结束刷新控件
            self.refreshControl?.endRefreshing()
            
            //恢复上拉刷新标记
            self.isPullup = false
            
            //刷新表格
            if shouldRefresh{
                self.tableView?.reloadData()
            }
            
        }
//        WBNetworkManager.shared.statusList { (list, isSuccess) in
//            
//            //字典转模型，绑定表格数据
//            print(list ?? "no data")
//        }
//        //延迟加载
//        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + DispatchTimeInterval.seconds(2)) {
//            for i in 0..<15{
//                if self.isPullup{
//                    //将数据追加到底部
//                    self.statusList.append("上拉 \(i)")
//                }else{
//                    //将数据插入到数组的顶部
//                    self.statusList.insert(i.description, at: 0)
//                }
//                
//            }
//            
//            //结束刷新控件
//            self.refreshControl?.endRefreshing()
//            
//            //恢复上拉刷新标记
//            self.isPullup = false
//            
//            //刷新表格
//            self.tableView?.reloadData()
//        }
        
        
    }
    
    @objc func showFriends(){
        print(#function)
        let vc = WBDemoViewController()
        vc.hidesBottomBarWhenPushed = true    //隐藏tabbar
        navigationController?.pushViewController(vc, animated: true)
    }
}

//MARK: - 表格数据源方法
extension WBHomeViewController{
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        //return statusList.count
        return listViewModel.statusList.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        //1.取cell
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
        
        //2. 设置cell
        cell.textLabel?.text = listViewModel.statusList[indexPath.row].text//statusList[indexPath.row]
        
        return cell
    }
}

//MARK: - 设置界面
extension WBHomeViewController{
    
    override func setupTableView() {
        super.setupTableView()
        
        navItem.leftBarButtonItem = UIBarButtonItem(title: "好友", fontSize: 14.0, target: self, action: #selector(showFriends))
        
        //注册原型cell
        tableView?.register(UITableViewCell.self, forCellReuseIdentifier: cellId)
        setupNavTitle()
    }
    
    fileprivate func setupNavTitle(){
        let title = WBNetworkManager.shared.userAccount.screen_name
        let button = WBTitleButton(title:title)
        navItem.titleView = button
        button.addTarget(self, action: #selector(clickTitleButton), for: UIControlEvents.touchUpInside)
    }
    
    @objc func clickTitleButton(btn:UIButton){
        btn.isSelected = !btn.isSelected
    }
//    
//    override func setupUI() {
//        super.setupUI()
//        
//        //无法实现点击时高亮
////        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "好友", style: UIBarButtonItemStyle.plain, target: self, action: #selector(showFriends))
//        navItem.leftBarButtonItem = UIBarButtonItem(title: "好友", fontSize: 14.0, target: self, action: #selector(showFriends))
//        
//        //注册原型cell
//        tableView?.register(UITableViewCell.self, forCellReuseIdentifier: cellId)
//    }
}
